package com.shanghai.shanghaielectric.UI.DilaogView;


import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

import com.shanghai.shanghaielectric.MyApplication;
import com.shanghai.shanghaielectric.R;

import java.lang.reflect.Field;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import shanghaielectric_lib.Utils.DateUtil;
import shanghaielectric_lib.Utils.MySystemUtil;


/**
 * Created by Administrator on 2017/9/2 0002.
 */

public class Dialog_datePick extends DialogFragment {


    @BindView(R.id.tv_title_datePick)
    TextView tvTitleDatePick;
    @BindView(R.id.bt_certain_datePick)
    Button btCertainDatePick;
    Unbinder unbinder;
    @BindView(R.id.dp_datepick)
    DatePicker dpDatepick;

    String time_date;
    String time_hh;
    String date;
    @BindView(R.id.tp_timepicker)
    TimePicker tpTimepicker;

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.bt_certain_datePick)
    public void onViewClicked() {
    }

    public interface Diolg_datePickListener {
        void onClink(String date);
    }

    private static Diolg_datePickListener mDiolg_datePickListener;

    public static void setDiolgAlertListener(Diolg_datePickListener diolgDatePickListener) {
        mDiolg_datePickListener = diolgDatePickListener;
    }


    @OnClick({R.id.bt_certain_datePick})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_certain_datePick:
                mDiolg_datePickListener.onClink(date);
                dismiss();
                break;
        }
    }

    public Context mContext;

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(R.layout.m_dialog_datepick, container);
        unbinder = ButterKnife.bind(this, view);
//        Toast.makeText(mContext, this.getTag().toString(), Toast.LENGTH_SHORT).show();
        time_date = DateUtil.getSystemTime_yyyy_MM_dd();
        time_hh = DateUtil.getSystemTime_HH() + ":00";
        date = DateUtil.getSystemTime_yyyy_MM_dd_HH() + ":00";
        initView();
        initData();
        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private void initView() {

        tpTimepicker.setIs24HourView(true);
        tpTimepicker.setCurrentMinute(0);
        hideMin(tpTimepicker);


        tvTitleDatePick.setText(commandName);
        dpDatepick.getCalendarView().setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(@NonNull CalendarView calendarView, int i, int i1, int i2) {
                time_date = i + "-" + String.format("%02d", i1 + 1) + "-" + String.format("%02d", i2);
                date = time_date + " " + time_hh;
            }
        });

        tpTimepicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker timePicker, int i, int i1) {
                time_hh = String.format("%02d", i) + ":00";
                date = time_date + " " + time_hh;
            }
        });
    }

    public String commandName;

    public void setCommandName(String name) {
        this.commandName = name;
    }

    private void initData() {
        mContext = MyApplication.getContext();
    }

    private void hideMin(TimePicker timePicker2) {
        Field[] datePickerfFields = timePicker2.getClass().getDeclaredFields();
        for (Field datePickerField : datePickerfFields) {
            Log.e("ttttttttttt", "" + datePickerField.getName());
            if ("mMinuteSpinner".equals(datePickerField.getName())) {
//这里这个变量是系统的定义的变量，不是自己定义的​,点击时间源码进去就可以看到很多定义的属性，这个就是分钟属性，控制这个就可以了
                datePickerField.setAccessible(true);
                Object dayPicker = new Object();
                try {
                    dayPicker = datePickerField.get(timePicker2);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                }
                //datePicker.getCalendarView().setVisibility(View.GONE);
//                ((View)dayPicker).setVisibility(View.GONE);
                ((View) dayPicker).setVisibility(View.GONE);
            }


        }
    }


}
